package com.yupi.oj.judge.strategy;
import cn.hutool.json.JSONUtil;
import com.yupi.oj.model.dto.question.JudgeConfig;
import com.yupi.oj.model.dto.questionSubmit.JudgeInfo;
import com.yupi.oj.model.entity.Question;
import com.yupi.oj.model.enums.JudgeQuestionMessage;

import java.util.List;

public class CJudge implements JudgeStrategy{
    @Override
    public String doJudge(JudgeContext judgeContext) {
        List<String> outputList = judgeContext.getOutputList();
        List<String> rightOutputList = judgeContext.getRightOutputList();
        Question question = judgeContext.getQuestion();
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();

        JudgeQuestionMessage judgeQuestionMessage = JudgeQuestionMessage.ACCEPTED;
        if (outputList.size() != rightOutputList.size()) {
            judgeQuestionMessage = JudgeQuestionMessage.WRONG_ANSWER;
            judgeInfo.setMessage(judgeQuestionMessage.getValue());
            return judgeInfo.getMessage();
        }

        //依次比较输出结果xzssdx
        for (int i = 0; i < outputList.size(); i++) {
            if (!outputList.get(i).contains(rightOutputList.get(i))) {
                judgeQuestionMessage = JudgeQuestionMessage.WRONG_ANSWER;
                judgeInfo.setMessage(judgeQuestionMessage.getValue());
                return judgeInfo.getMessage();
            }
        }
        //比较限制是否一样
        Long time = judgeInfo.getTime();
        Long memory = judgeInfo.getMemory();
        String judgeConfig = question.getJudgeConfig();
        JudgeConfig bean = JSONUtil.toBean(judgeConfig, JudgeConfig.class);
        Long timeLimit = bean.getTimeLimit();
        Long memoryLimit = bean.getMemoryLimit();
        if (time!=null && time > timeLimit) {
            judgeQuestionMessage = JudgeQuestionMessage.TIME_LIMIT_EXCEEDED;
            judgeInfo.setMessage(judgeQuestionMessage.getValue());
            return judgeInfo.getMessage();
        }
        if (memory!=null && memory > memoryLimit) {
            judgeQuestionMessage = JudgeQuestionMessage.MEMORY_LIMIT_EXCEEDED;
            judgeInfo.setMessage(judgeQuestionMessage.getValue());
            return judgeInfo.getMessage();
        }
        judgeInfo.setMessage(judgeQuestionMessage.getValue());
        return judgeInfo.getMessage();
    }
}
